package com.yichi;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.cretin.www.cretinautoupdatelibrary.utils.AppUpdateUtils;
import com.yichi.service.DeviceService;
import com.yichi.util.ToastUtil;
import com.yichi.util.Utils;

import java.util.ArrayList;

public class SettingActivity extends AppCompatActivity {

    private ImageView iv_back;
    private RelativeLayout rl_url, rl_autoupdate, rl_clear_data, rl_mode;
    private TextView tv_version_name;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_setting);

        rl_url = findViewById(R.id.rl_url);
        rl_mode = findViewById(R.id.rl_mode);
        rl_clear_data = findViewById(R.id.rl_clear_data);
        iv_back = findViewById(R.id.iv_back);
        rl_autoupdate = findViewById(R.id.rl_autoupdate);
        tv_version_name = findViewById(R.id.tv_version_name);

        String appVersionName = Utils.getAppVersionName(SettingActivity.this);
        tv_version_name.setText(String.format("当前版本：v%s", appVersionName));

        //返回
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //服务端地址
        rl_url.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(SettingActivity.this, AddressActivity.class));
            }
        });
        //电池仓管理
        rl_mode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(SettingActivity.this, ModeActivity.class));
            }
        });
        //清除系统数据
        rl_clear_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handleClearData();
            }
        });
        //自动更新
        rl_autoupdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AppUpdateUtils.getInstance().checkUpdate();
            }
        });
    }

    private void handleClearData() {
        AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
        builder.setTitle("确认操作")
                .setMessage("重置后，必须手动取出柜内所有电池，并重启APP，以免发生撞车！！！")
                .setPositiveButton("确认", (dialog, which) -> {
                    DeviceService.inst.clearData();
                    ToastUtil.show(SettingActivity.this, "重置已成功，请清空柜内所有电池！！！！！！");
                })
                .setNegativeButton("取消", (dialog, which) -> {
                })
                .show(); // 显示对话框
    }
}
